<?php
defined('_JEXEC') or die();

require_once JPATH_COMPONENT.DS.'controller.php';

class CauhoiControllerCauhoi extends JController
{
    
    public $msg; 
    //$link = 'index.php?option=com_cauhoi';
    
    public function display($cachable = false, $urlparams = false) {
        
        parent::display($cachable, $urlparams);
    }
    
    
    public function luu()
    {
        JSession::checkToken('post') or jexit(JText::_('JInvalid_Token'));
        if(isset($_POST["task2"]))
        {
            $this->display();
            return;
        }
        
        if(isset($_POST["task"]))  
            {
               
                if($_POST["noidung"]=="")               
                {
                    $app = &JFactory::getApplication();   
                    $app->enqueueMessage(JText::_('Bạn chưa nhập nội dung câu hỏi'));
                    $this->display();
                    return;
                }
                               
                if($_POST["luachon"])
                {
                    //print_r($_POST["luachon"]);
                    $listdapan="";
                    $i=0;
                    $dapan = $_POST["luachon"];
                   foreach ($dapan as $row) 
                   {
                       if($row!="")
                       {
                           $listdapan[$i]=$row;
                           $i++;
                       }
                   }
                   //print_r($listdapan);
                }
                //print_r($_POST["luachon"]);
            //$chudecon = $_POST["chudecon"];
            //echo $chudecon;
                //print_r($_POST["noidung"]);
                //print_r($_POST["chude"]);
                //print_r($_POST["luachon"]);
                $chude = $_POST["chude"];
                $noidung=$_POST["noidung"];
                $datthieu = $_POST["datoithieu"];
                $datda =$_POST["datoida"];
               // print_r($datthieu);
                //print_r($datda);
                //print_r($model);
                /*if($datthieu>$datda)
                {
                    $app = &JFactory::getApplication();   
                    $app->enqueueMessage(JText::_('Đáp án tối thiểu phải nhỏ hơn đáp án tối đa'));
                    $this->display();
                    return;
                    
                }*/
                $model = $this->getModel();   
                //kiem tra noi dung cau hoi co bi trung
                if( $model->kiemtra($noidung)==FALSE)
                {
                    
                    $app = &JFactory::getApplication();   
                    $app->enqueueMessage(JText::_('Câu hỏi đã có !!! Bạn hãy tạo câu hỏi khác'));
                    $this->display();
                    return;
                }
                //them
                $model->them($noidung,$listdapan,$chude,$datthieu,$datda);
                //hien message
                $app = &JFactory::getApplication();   
                $app->enqueueMessage(JText::_('Thêm thành công'));
                
               // print_r($cauhoi);
                //chuyen noi dung chu de ra gaio dien

                $this->display();
        }             
    }
    public function huy($message)
    {
        JRequest::checkToken() or jexit( 'Invalid Token' );
        //hien message
        $app = &JFactory::getApplication();
        $app->enqueueMessage(JText::_($message));
        $this->display();
    }
    
}

